package com.tanhua.app.controllerservice;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.tanhua.api.db.UserInfoApi;
import com.tanhua.api.mongo.InteractionApi;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Interaction;
import com.tanhua.domain.vo.CommentVo;
import com.tanhua.domain.vo.PageBeanVo;
import com.tanhua.domain.vo.UserHolder;
import com.tanhua.utils.RelativeDateFormat;
import org.apache.dubbo.config.annotation.Reference;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class InteractionService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Reference
    private UserInfoApi userInfoApi;

    @Reference
    private InteractionApi interactionApi;
    @CacheEvict(value = "video",allEntries = true)
    public ResponseEntity unLike(String videoId) {
        Interaction interaction = new Interaction();
        interaction.setVideoId(new ObjectId(videoId));
        interaction.setInteractionType(1);
        interaction.setIntType(2);
        interaction.setUserId(UserHolder.getUser().getId());
        Long count = interactionApi.shiping(interaction);
        stringRedisTemplate.opsForValue().set(StrUtil.format("video_like_interaction_{}_{}", UserHolder.getUser().getId(), videoId), "1");
        return ResponseEntity.ok(count.intValue());
    }
    @CacheEvict(value = "video",allEntries = true)
    public ResponseEntity like(String videoId) {
        Interaction interaction = new Interaction();
        interaction.setVideoId(new ObjectId(videoId));
        interaction.setInteractionType(1);
        interaction.setIntType(2);
        interaction.setUserId(UserHolder.getUser().getId());
        Long count = interactionApi.shiping2(interaction);
        stringRedisTemplate.delete( StrUtil.format("video_like_interaction_{}_{}",UserHolder.getUser().getId(),videoId));
        return ResponseEntity.ok(count.intValue());
    }
    @CacheEvict(value = "video",allEntries = true)
    public ResponseEntity pllb(String commentsId,Integer page, Integer pagesize) {
        PageBeanVo pageBeanVo = interactionApi.findInteraction(commentsId,page, pagesize);
        List<Interaction> items = (List<Interaction>)pageBeanVo.getItems();
        List<CommentVo> commentVoList = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(items)){
        for (Interaction interaction : items) {
            UserInfo userInfo = userInfoApi.findUserInfo(interaction.getUserId());
            CommentVo vo = new CommentVo();
            vo.setAvatar(userInfo.getAvatar());
            vo.setNickname(userInfo.getNickname());
            vo.setId(interaction.getId().toString());
            vo.setContent(interaction.getContent());
            vo.setLikeCount(interaction.getLikeCount());
            vo.setCreateDate(RelativeDateFormat.format(new Date(interaction.getCreated())));
            commentVoList.add(vo);
        }
        }
        pageBeanVo.setItems(commentVoList);
        return ResponseEntity.ok(pageBeanVo);
    }
    @CacheEvict(value = "video",allEntries = true)
    public ResponseEntity pinglun(String videoId, String content) {
        Interaction interaction = new Interaction();
        interaction.setVideoId(new ObjectId(videoId));
        interaction.setInteractionType(2);
        interaction.setContent(content);
        interaction.setIntType(2); // 2 小视频
        interaction.setUserId(UserHolder.getUser().getId());
        Long count = interactionApi.shiping(interaction);
        return ResponseEntity.ok(count.intValue());
    }
}
